﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TomatoClock.Models;
using TomatoClock.Services;
using TomatoClock.ViewModels;

namespace TomatoClock.Views
{
    public partial class ProcessManagerControl : UserControl
    {
        List<ForbiddenAppGroup> forbiddenAppGroups;
        ProcessManager processManager;
        private MainViewModel viewModel;
        private MaterialSkin.MaterialListBoxItem selectedGroup;
        private MaterialSkin.MaterialListBoxItem selectedProcess;
        private MoniterForm moniterForm;
        public ProcessManagerControl(MainViewModel viewModel)
        {
            InitializeComponent();
            this.viewModel = viewModel;
            this.processManager = new ProcessManager(viewModel);
            UpdateForbiddenGroupsList();  // 确保初始化时加载数据
        }

        private void btnAddGroup_Click(object sender, EventArgs e)
        {
            // 弹出小窗口输入新组名
            using (var form = new AddGroupNameForm())
            {
                if (form.ShowDialog() == DialogResult.OK)
                {
                    // 获取新组名并执行添加操作
                    string newGroupName = form.GroupName;
                    // 调用 ProcessManager 方法添加组
                    processManager.CreateGroup(newGroupName);
                    // 更新列表框
                    UpdateForbiddenGroupsList();
                }
            }
        }
        private void btnDeleteGroup_Click(object sender, EventArgs e)
        {
            setSelected();
            if (selectedGroup != null)
            {
                string group = selectedGroup.Text;
                processManager.DeleteGroup(group);  // 调用 ProcessManager 方法删除组
                UpdateForbiddenGroupsList();  // 更新列表框
            }
            else
            {
                // 弹出小窗口输入要删除的组名
                using (var form = new DeleteGroupNameForm(processManager))
                {
                    if (form.ShowDialog() == DialogResult.OK)
                    {
                        // 获取要删除的进程组名集合
                        List<string> groups = form.GetSelectedGroupNames();
                        // 调用 ProcessManager 方法删除组
                        foreach (var group in groups)
                        {
                            processManager.DeleteGroup(group);
                        }
                        // 更新列表框
                        UpdateForbiddenGroupsList();
                    }
                }
            }
        }

        private void btnAddProcess_Click(object sender, EventArgs e)
        {
            // 弹出小窗口输入新进程信息
            using (var form = new AddProcessForm(processManager))
            {
                if (form.ShowDialog() == DialogResult.OK)
                {
                    // 获取新进程名
                    string processName = form.ProcessName;
                    if (lstForbiddenGroups.SelectedItem != null)
                    {
                        string groupname = lstForbiddenGroups.SelectedItem.Text;    // 获取当前选中的禁止程序组
                        processManager.AddProcess(processName);  //添加进程
                        processManager.AddGroupProcess(groupname, processName);       // 添加组-进程关联
                        MaterialSkin.MaterialListBoxItem item = new MaterialSkin.MaterialListBoxItem(processName);
                        lstProcessesInGroup.Items.Add(item);
                    }
                }
            }
        }
        private void setSelected()
        {
            selectedGroup = lstForbiddenGroups.SelectedItem;
            selectedProcess = lstProcessesInGroup.SelectedItem;
        }
        private void btnDeleteProcess_Click(object sender, EventArgs e)
        {
            setSelected();
            if (selectedProcess != null)
            {
                processManager.DeleteGroupProcess(selectedGroup.Text, selectedProcess.Text);
                UpdateProcessInGroups();
            }
            else
            {
                if (lstForbiddenGroups.SelectedItem != null)
                {
                    using (var form = new DeleteProcessForm(lstForbiddenGroups.SelectedItem.Text, processManager))
                    {
                        if (form.ShowDialog() == DialogResult.OK)
                        {
                            List<string> processNames = form.GetSelectedProcessNames();
                            if (lstForbiddenGroups.SelectedItem != null)
                            {
                                string groupname = lstForbiddenGroups.SelectedItem.Text;
                                foreach (var process in processNames)
                                {
                                    processManager.DeleteGroupProcess(groupname, process);                  // 调用 ProcessManager 方法删掉进程组
                                    processManager.DeleteGroup(groupname);
                                }
                            }
                        }
                    }
                }
            }
        }
        private void UpdateProcessList()
        {
            if (lstForbiddenGroups.SelectedItem != null)
            {
                string groupname = lstForbiddenGroups.SelectedItem.Text;
                List<BanGroup> banGroups = processManager.LoadUserGroups(processManager.UserId);

                lstProcessesInGroup.Items.Clear();

                foreach (var group in banGroups)
                {
                    if (group.GroupName.Equals(groupname))
                    {
                        foreach (var processId in group.Processes)
                        {
                            string processName = processManager.ProcessChangeToString(processId);
                            MaterialSkin.MaterialListBoxItem item = new MaterialSkin.MaterialListBoxItem(processName);
                            lstProcessesInGroup.Items.Add(item);
                        }
                        break;
                    }
                }
            }
        }





        private void materialListBox2_SelectedIndexChanged(object sender, MaterialSkin.MaterialListBoxItem selectedItem)
        {
            if (lstProcessesInGroup.SelectedItem != null)
            {
                selectedProcess = lstProcessesInGroup.SelectedItem;
            }
        }
        private void UpdateProcessInGroups()
        {
            if ((lstProcessesInGroup != null))
            {
                lstProcessesInGroup.Clear();
            }
            if (lstForbiddenGroups.SelectedItem != null)
            {
                List<string> ProcessesName = processManager.LoadProcesses(lstForbiddenGroups.SelectedItem.Text);
                foreach (var process in ProcessesName)
                {
                    var item = new MaterialSkin.MaterialListBoxItem(process);
                    lstProcessesInGroup.Items.Add(item);
                }
            }
        }
        private void UpdateForbiddenGroupsList()
        {
            // 获取当前用户的所有禁止程序组
            List<BanGroup> banGroups = processManager.LoadUserGroups(processManager.UserId);
            lstForbiddenGroups.Items.Clear();

            // 更新界面上的列表
            foreach (var group in banGroups)
            {
                var item = new MaterialSkin.MaterialListBoxItem(group.GroupName);
                lstForbiddenGroups.Items.Add(item);
            }

            Debug.WriteLine("Forbidden groups list updated successfully.");
        }

        private void lstProcessesInGroup_SelectedIndexChanged(object sender, MaterialSkin.MaterialListBoxItem selectedItem)
        {
            // 检查是否有选定的禁止组
            if (lstForbiddenGroups.SelectedItem != null)
            {
                try
                {
                    // 获取选定组中的进程
                    List<BanGroup> banGroups = processManager.LoadUserGroups(processManager.UserId);

                    // 检查banGroups是否为空
                    if (banGroups == null)
                    {
                        // 记录日志或处理错误
                        Console.WriteLine("BanGroups is null");
                        return;
                    }

                    string groupname = lstForbiddenGroups.SelectedItem.Text;

                    // 清空进程列表框
                    lstProcessesInGroup.Items.Clear();

                    // 加载新进程列表
                    List<string> newProcesses = processManager.LoadProcesses(groupname);

                    // 检查newProcesses是否为空
                    if (newProcesses == null)
                    {
                        // 记录日志或处理错误
                        Console.WriteLine("NewProcesses is null");
                        return;
                    }

                    // 更新进程列表框
                    foreach (var str in newProcesses)
                    {
                        if (str != null)
                        {
                            MaterialSkin.MaterialListBoxItem item = new MaterialSkin.MaterialListBoxItem(str);
                            lstProcessesInGroup.Items.Add(item);
                        }
                        else
                        {
                            // 记录日志或处理空字符串
                            Console.WriteLine("Encountered null process string");
                        }
                    }
                }
                catch (Exception ex)
                {
                    // 捕获并记录异常
                    Console.WriteLine($"Exception occurred: {ex.Message}");
                }
            }
            else
            {
                // 没有选定的禁止组时，清空进程列表框
                lstProcessesInGroup.Items.Clear();
            }
        }


        private void btnSelecteGroup_Click(object sender, EventArgs e)
        {
            using (var form = new SelecteGroupForm(processManager))
            {
                if (form.ShowDialog() == DialogResult.OK)
                {
                    string selectedGroups = form.GetSelectedGroupNames();
                    processManager.SelectBanGroups(selectedGroups);
                }
            }
        }

        private void RefreshButton6_Click(object sender, EventArgs e)
        {
            UpdateForbiddenGroupsList();
            UpdateProcessInGroups();
        }
    }
}

